package com.epoch.server.kernel.serve;

import com.epoch.common.Message;
import com.epoch.common.MessageType;
import com.epoch.common.User;
import com.epoch.common.UserRegister;
import com.epoch.common.loader.ParameterLoader;
import com.epoch.server.kernel.dao.UserDao;
import lombok.extern.slf4j.Slf4j;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 主服务器
 */
@Slf4j
public class EchoServer {
    Object o;

    private final UserDao userDao = new UserDao();

    private Map<String, Object> map;
    public EchoServer() {
        try {
            //实例化ServerSocket并创建端口
            ServerSocket serverSocket = new ServerSocket(ParameterLoader.PORT);
            while (true) {
                log.info("服务器套接字已经启动...");
                log.info("Finding answer...");
                //获取socket
                Socket accept = serverSocket.accept();
                //读取输入流对象
                ObjectInputStream ois = new ObjectInputStream(accept.getInputStream());
                o = ois.readObject();
                log.info("服务器收到对象流...");

                if(o instanceof User) {
                    User u = (User) o;
                    Message message = new Message();
                    String username = u.getName();
                    String password = u.getPassword();
                    User loginUser = userDao.userCheck(username, password);
                    if (loginUser != null) {
                        map = new HashMap<>();
                        map.put("user", loginUser);
                        message.setDate(new Date());
                        message.setMessage("success");
                        message.setSender(8000);
                        message.setMessageType(MessageType.MESSAGE_LOGIN_SUCCESS);
                        message.setDynamicData(map);

                        //准备开启群聊的线程
                        GroupChatConnThread thread = new GroupChatConnThread(accept, message.getGroupId());
                        ClientConnManager.addClientThread(username, thread);
                        thread.start();
                        log.info(username + "用户已上线...");
                    } else {
                        //设置登陆失败的信息
                        message.setMessageType(MessageType.MESSAGE_LOGIN_FAIL);
                        message.setSender(1);
                        message.setDate(new Date());
                        message.setMessage(username + "登录失败！");
                    }
                    ObjectOutputStream oos = new ObjectOutputStream(accept.getOutputStream());
                    oos.writeObject(message);
                } else if (o instanceof UserRegister) {
                    UserRegister register = (UserRegister) o;
                    Message message = new Message();
                    //如果读取到的对象是UserLogup，对UserLogup的信息在数据库进行注册
                    if (userDao.registerUser(register)) {
                        //注册成功后，回送一条信息给客户端
                        message.setMessageType(MessageType.MESSAGE_REGISTER_SUCCESS);
                        message.setDate(new Date());
                        message.setSenderName(register.getName());
                        message.setMessage("注册成功！");
                    } else {
                        message.setMessageType(MessageType.MESSAGE_REGISTER_FAIL);
                        message.setMessage("注册失败！");
                    }
                    ObjectOutputStream oos = new ObjectOutputStream(accept.getOutputStream());
                    oos.writeObject(message);
                }
            }

        } catch (Exception e) {
            log.info(e.getMessage());
            log.info(e.getLocalizedMessage());
            e.printStackTrace();
        } finally {
            log.info("又g了");
        }
    }
}
